package model.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import model.bean.ChuyenAn;

public class ChuyenAnDAO extends BaseDAO {

	public int getChuyenanID() {
		openDatabase();
		qry = "SELECT idchuyen_an FROM mockdb.chuyen_an ORDER BY idchuyen_an DESC LIMIT 1";
		try {
			pst = con.prepareStatement(qry);
			rs = pst.executeQuery();

			int id;
			while (rs.next()) {
				id = rs.getInt("idchuyen_an");
				int idChuyenan = id + 1;
				return idChuyenan;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeDatabase();
		}
		return 1;
	}

	public boolean themChuyenAn(int id, String tenChuyenan, String ghichu) {
		boolean success = false;
		openDatabase();
		qry = "INSERT INTO mockdb.chuyen_an (idchuyen_an,tenchuyenan,ghichu) VALUES('" + id
				+ "',N'" + tenChuyenan + "',N'" + ghichu + "')";
		
		try {
			pst = con.prepareStatement(qry);
			pst.executeUpdate();
			success = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return success;
	}

	public ArrayList<ChuyenAn> dsChuyenan() {
		ArrayList<ChuyenAn> dsChuyenan = new ArrayList<ChuyenAn>();
		openDatabase();
		qry = "SELECT * FROM mockdb.chuyen_an";
		try {
			pst = con.prepareStatement(qry);
			rs = pst.executeQuery();
			while (rs.next()) {
				ChuyenAn ca = new ChuyenAn();
				
				ca.setIdChuyenan(rs.getString("idchuyen_an"));
				ca.setTenChuyenAn(rs.getString("tenchuyenan"));
				ca.setGhiChu(rs.getString("ghichu"));
				
				dsChuyenan.add(ca);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return dsChuyenan;
	}

	public boolean xoaChuyenan(String id) {
		openDatabase();
		qry = "DELETE FROM mockdb.chuyen_an WHERE idchuyen_an = '" + id + "'";
		try {
			pst = con.prepareStatement(qry);
			pst.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

	public ChuyenAn getChuyenan(String id) {
		ChuyenAn chuyenAn = new ChuyenAn();
		openDatabase();
		qry = "select * from mockdb.chuyen_an where idchuyen_an = '" + id + "'";
		try {
			pst = con.prepareStatement(qry);
			rs = pst.executeQuery();

			while (rs.next()) {
				chuyenAn.setIdChuyenan(rs.getString("idchuyen_an"));
				chuyenAn.setTenChuyenAn(rs.getString("tenchuyenan"));
				chuyenAn.setGhiChu(rs.getString("ghichu"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return chuyenAn;
	}

	public boolean capnhapCA(String id, String tenca, String ghichu) {
		openDatabase();
		qry = "UPDATE mockdb.chuyen_an SET tenchuyenan = '" + tenca
				+ "', ghichu = '" + ghichu + "' WHERE idchuyen_an = '" + id + "'";
		try {
			pst = con.prepareStatement(qry);
			pst.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}
}
